package corestruct

type Page[T any] struct {
	PageNumber int   `json:"pageNumber"   gorm:"column:page_number;DEFAULT 0;not null;comment:当前页码;"`
	PageSize   int   `json:"pageSize"   gorm:"column:page_size;DEFAULT 0;not null;comment:每页条数;"`
	TotalPage  int64 `json:"totalPage"   gorm:"column:total_page;DEFAULT 0;not null;comment:总页数;"`
	TotalRow   int64 `json:"totalRow"   gorm:"column:total_row;DEFAULT 0;not null;comment:总记录数;"`
	Records    []T   `json:"records"   gorm:"-;comment:记录;"`
}

func (p *Page[T]) GetOffset() int {
	return int(p.PageSize * (p.PageNumber - 1))
}
func NewPage[T any](totalRow int64, pageNumber, pageSize int) *Page[T] {
	var p = &Page[T]{}
	p.TotalRow = totalRow
	p.PageNumber = pageNumber
	p.PageSize = pageSize
	p.Records = make([]T, 0)
	if p.PageSize == 0 {
		return p
	}
	p.TotalPage = p.TotalRow / int64(p.PageSize)
	if p.TotalRow%int64(p.PageSize) != 0 {
		p.TotalPage++
	}
	return p
}
